package com.ybt.service.impl.base;

import com.ruleengine.context.IContextProperty;
import com.ruleengine.context.IWorkflowConext;
import com.ruleengine.endpoint.data.IEndpointData;
import com.ybt.context.IYBTContextConstants;
import com.ybt.message.manager.PackUnpackStrategyManager;
import com.ybt.message.pack.IMessagePack;
import com.ybt.message.pack.exception.MessagePackException;
import com.ybt.service.exception.ServiceException;
import com.ybt.service.impl.BlockingBaseService;

/**
 * 通用打包服务
 * 
 * @author AB033862 Jul 31, 2012
 */
public class PackService extends BlockingBaseService {

    @Override
    public void service(IWorkflowConext context, IEndpointData data)
            throws ServiceException {

        // 渠道
        IContextProperty<String> channelp = context
                .getContextProperty(IYBTContextConstants.YBT_CHANNEL);
        String channel = channelp.getValue();

        // 获取拆包实现类
        IMessagePack pack = PackUnpackStrategyManager.getInstance()
                .getMessagePack(channel);
        try {
            // 调用打包实现
            Object result = pack.pack(context, data.getOutData());
            // 原始报文
            IContextProperty<Object> op = context
                    .getContextProperty(IYBTContextConstants.ORIGINAL_DATA);
            op.setValue(result);
        } catch (MessagePackException e) {
            throw new ServiceException(e);
        }
    }

}
